package com.zhl.multi_data_source_demo.service;

import com.alibaba.fastjson.JSONArray;
import com.zhl.multi_data_source_demo.DataSource;
import com.zhl.multi_data_source_demo.DataSourceAspect;
import com.zhl.multi_data_source_demo.DataSourceHolder;
import com.zhl.multi_data_source_demo.dao.UserinfoDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

@org.springframework.stereotype.Service
public class Service {
    @Autowired
    UserinfoDAO userinfoDAO;
    @Autowired
    private ApplicationContext applicationContext;

    private static Logger logger = LoggerFactory.getLogger(DataSourceAspect.class);

    public void contextLoads() {
        DataSourceHolder.setDataSource(DataSource.slave);
        //javax.sql.DataSource dataSource = (javax.sql.DataSource) applicationContext.getBean("DataSource");
        System.out.println(JSONArray.toJSONString(userinfoDAO.findAll()));
        //logger.info("当前数据源：{}",dataSource);
    }

    @DataSource(DataSource.slave)
    public void contextLoads2() {
        System.out.println(JSONArray.toJSONString(userinfoDAO.findAll2()));
    }

    public void contextLoads21() {
        System.out.println(JSONArray.toJSONString(userinfoDAO.findAll2()));

    }
    public void contextLoads3() {
        DataSourceHolder.setDataSource(DataSource.master);
        //javax.sql.DataSource dataSource = (javax.sql.DataSource) applicationContext.getBean("DataSource");
        System.out.println(JSONArray.toJSONString(userinfoDAO.findAll3()));
        //logger.info("当前数据源：{}",dataSource);
    }
}
